package com.bot.adms.service.impl;

import com.bot.adms.dao.*;
import com.bot.adms.dao.impl.*;
import com.bot.adms.model.bo.AdInfoBo;
import com.bot.adms.model.entity.*;
import com.bot.adms.model.vo.*;
import com.bot.adms.service.IAdService;

import java.util.ArrayList;
import java.util.List;

/**
 * 广告模块业务层接口实现
 * 包含：
 * 删除广告
 * 查看广告
 * 查询广告
 * 下线广告
 * 上线广告
 * 预览广告
 */
public class AdServiceImpl implements IAdService {


    /**
     * 删除广告
     * @param id 删除广告的id标识
     * @return是否成功
     */
    @Override
    public Boolean deleteAd(Long id) {
        long delApplyId = id;
        long delAdId = 0;
        long delAdSpScDetailId = 0;
        int flag=0;

        //1.广告主表操作
        //1.1根据广告id查询广告分屏明细下的内容
        AdSpScDetail adSpScDetail = new AdSpScDetail();
        adSpScDetail.setAdId(id);
        IAdSpScDetailDao adSpScDetailDao = new AdSpScDetailDaoImpl();
        List<AdSpScDetail> adSpScDetails = adSpScDetailDao.searchAdSpScDetail(adSpScDetail);

        //1.2遍历查找到的数据，将分屏id存储，并根据分屏明细id查找对应内容id
        for(int i=0; i<adSpScDetails.size(); i++){
            delAdSpScDetailId = adSpScDetails.get(i).getId();
            //1.3根据detailId查询内容
            //文字
            if (adSpScDetails.get(i).getType()==0){

                //将分屏id封装为对象
                AdSpScDetailText adSpScDetailText = new AdSpScDetailText();
                adSpScDetailText.setAdSpScDetailId(delAdSpScDetailId);
                //查找分屏号下的数据
                IAdSpScDetailTextDao adSpScDetailTextDao = new AdSpScDetailTextDaoImpl();
                List<AdSpScDetailText> adSpScDetailTexts = adSpScDetailTextDao.searchAdSpScDetailText(adSpScDetailText);
                long delAdSpScDetailTextId = adSpScDetailTexts.get(0).getId();
                //根据id将对应数据删除
                Integer i1 = adSpScDetailTextDao.deleteAdSpScDetailText(delAdSpScDetailTextId);
                if(i1>0){
                    //删除对应上级表
                    Integer i2 = adSpScDetailDao.deleteAdSpScDetail(delAdSpScDetailId);
                    if (i2>0){
                        flag ++;
                    }
                }
            }
            //海报
            else if (adSpScDetails.get(i).getType()==1){
                //将分屏id封装成对象
                AdSpScDetailPoster adSpScDetailPoster = new AdSpScDetailPoster();
                adSpScDetailPoster.setAdSpScDetailId(delAdSpScDetailId);
                //查找分屏号下的数据
                IAdSpScDetailPosterDao adSpScDetailPosterDao = new AdSpScDetailPosterDaoImpl();
                List<AdSpScDetailPoster> adSpScDetailPosters = adSpScDetailPosterDao.searchAdSpScDetailPoster(adSpScDetailPoster);
                long delAdSpScDetailPosterId = adSpScDetailPosters.get(0).getId();
                //根据id删除海报内容
                Integer i1 = adSpScDetailPosterDao.deleteAdSpScDetailPoster(delAdSpScDetailPosterId);
                if(i1>0){
                    //删除对应上级表
                    Integer i2 = adSpScDetailDao.deleteAdSpScDetail(delAdSpScDetailId);
                    if (i2>0){
                        flag ++;
                    }
                }
            }
            //视频
            else if (adSpScDetails.get(i).getType()==2){
                //将分屏id封装成数据
                AdSpScDetailVideo adSpScDetailVideo = new AdSpScDetailVideo();
                adSpScDetailVideo.setAdSpScDetailId(delAdSpScDetailId);
                //查找分屏号下的数据
                IAdSpScDetailVideoDao adSpScDetailVideoDao = new AdSpScDetailVideoDaoImpl();
                List<AdSpScDetailVideo> adSpScDetailVideos = adSpScDetailVideoDao.searchAdSpScDetailVideo(adSpScDetailVideo);
                long delAdSpScDetailVideoId = adSpScDetailVideos.get(0).getId();
                //根据id删除视频
                Integer i1 = adSpScDetailVideoDao.deleteAdSpScDetailVideo(delAdSpScDetailVideoId);
                if(i1>0){
                    //删除对应上级表
                    Integer i2 = adSpScDetailDao.deleteAdSpScDetail(delAdSpScDetailId);
                    if (i2>0){
                        flag ++;
                    }
                }
            }
        }
        //3验证广告分屏表是否删除完全，删除完全之后删除广告表
        if (flag == adSpScDetails.size()){
            IAdInfoDao adInfoDao = new AdInfoDaoImpl();
            Integer i = adInfoDao.deleteAdInfo(id);
            if (i>0){
                return true;
            }
        }
        return false;
    }

    /**
     * 查看广告
     * @param id 查看广告的id标识
     * @return 查询到的广告信息的vo视图模型
     */
    @Override
    public AdInfoVo getAd(Long id) {

        AdInfoVo adInfoVo = new AdInfoVo();
        AdInfoBo adInfoBo = new AdInfoBo();
        adInfoBo.setId(id);
        List<AdInfoVo> adInfoVos = this.getAdList(adInfoBo);
        if (adInfoVos.size()>0){
            adInfoVo = adInfoVos.get(0);
        }
        return adInfoVo;
    }

    /**
     * 预览广告
     *
     * @param id 查看广告的id标识
     * @return 需要预览的广告信息的vo视图模型
     */
    @Override
    public AdInfoVo previewAd(Long id) {
        AdInfoVo adInfoVo = new AdInfoVo();
        AdInfoBo adInfoBo = new AdInfoBo();
        adInfoBo.setId(id);
        List<AdInfoVo> adInfoVos = this.getAdList(adInfoBo);
        if (adInfoVos.size()>0){
            adInfoVo = adInfoVos.get(0);
        }
        return adInfoVo;
    }

    /**
     * 查询广告
     * @param adInfoBo 查询的广告条件，包含用标题名，状态
     * @return搜索到的广告的信息的视图数据集合
     */
    @Override
    public List<AdInfoVo> getAdList(AdInfoBo adInfoBo) {
        //1.实体化数据
        AdInfo adInfo = new AdInfo();
        adInfo.setAdTitle(adInfoBo.getAdTitle());
        adInfo.setSpScType(adInfoBo.getSpScType());
        adInfo.setLayStyle(adInfoBo.getLayStyle());
        adInfo.setStatus(adInfoBo.getStatus());
        adInfo.setCreateTime(adInfoBo.getCreateTime());
        adInfo.setId(adInfoBo.getId());
        //2.根据条查找数据
        IAdInfoDao adInfoDao = new AdInfoDaoImpl();
        List<AdInfo> adInfos = adInfoDao.searchAdInfo(adInfo);
        List<AdInfoVo> adInfoVos = new ArrayList<>();
        //3.遍历查找子表数据
        for (AdInfo adInfo1 : adInfos) {
            //1.将数据赋值给vo模型
            AdInfoVo adInfoVo = new AdInfoVo();
            adInfoVo.setId(adInfo1.getId());
            adInfoVo.setAdTitle(adInfo1.getAdTitle());
            adInfoVo.setSpScType(adInfo1.getSpScType());
            adInfoVo.setLayStyle(adInfo1.getLayStyle());
            adInfoVo.setStatus(adInfo1.getStatus());
            adInfoVo.setCreateTime(adInfo1.getCreateTime());
            //2。根据id查询子表
            IAdSpScDetailDao adSpScDetailDao = new AdSpScDetailDaoImpl();
            
            //2.1将id封装成分屏表对象
            AdSpScDetail adSpScDetail = new AdSpScDetail();
            adSpScDetail.setAdId(adInfo1.getId());
            List<AdSpScDetail> adSpScDetails = adSpScDetailDao.searchAdSpScDetail(adSpScDetail);
            //分屏明细vo
            List<AdSpScDetailVo> adSpScDetailVos = new ArrayList<>();
            //2.2将数据遍历添加到vo
            for (AdSpScDetail adSpScDetail1 : adSpScDetails) {
                AdSpScDetailVo adSpScDetailVo = new AdSpScDetailVo();
                adSpScDetailVo.setId(adSpScDetail1.getId());
                adSpScDetailVo.setType(adSpScDetail1.getType());
                adSpScDetailVo.setScId(adSpScDetail1.getScId());

                //2.3根据广告类型查找详细数据
                //文字
                if (adSpScDetailVo.getType()==0){
                    //1.将分屏id封装成对象
                    AdSpScDetailText adSpScDetailText = new AdSpScDetailText();
                    adSpScDetailText.setAdSpScDetailId(adSpScDetail1.getId());

                    //2.调用dao方法查找
                    IAdSpScDetailTextDao adSpScDetailTextDao = new AdSpScDetailTextDaoImpl();
                    List<AdSpScDetailText> adSpScDetailTexts = adSpScDetailTextDao.searchAdSpScDetailText(adSpScDetailText);
                    for (AdSpScDetailText adSpScDetailText1 : adSpScDetailTexts) {
                        AdSpScDetailTextVo adSpScDetailTextVo = new AdSpScDetailTextVo();
                        adSpScDetailTextVo.setId(adSpScDetailText1.getId());
                        adSpScDetailTextVo.setImageUrl(adSpScDetailText1.getImageUrl());
                        adSpScDetailTextVo.setLocation(adSpScDetailText1.getLocation());
                        adSpScDetailTextVo.setFontType(adSpScDetailText1.getFontType());
                        adSpScDetailTextVo.setFontSize(adSpScDetailText1.getFontSize());
                        adSpScDetailTextVo.setFontColor(adSpScDetailText1.getFontColor());
                        adSpScDetailTextVo.setTextContent(adSpScDetailText1.getTextContent());

                        //将查到的数据存储到文字对象
                        adSpScDetailVo.setAdSpScDetailTextVo(adSpScDetailTextVo);
                    }
                }//海报
                else if (adSpScDetailVo.getType()==1){
                    //将id封装成dao对象
                    AdSpScDetailPoster  adSpScDetailPoster = new AdSpScDetailPoster();
                    adSpScDetailPoster.setAdSpScDetailId(adSpScDetailVo.getId());
                    
                    //2.调用查找方法
                    IAdSpScDetailPosterDao adSpScDetailPosterDao = new AdSpScDetailPosterDaoImpl();
                    List<AdSpScDetailPoster> adSpScDetailPosters = adSpScDetailPosterDao.searchAdSpScDetailPoster(adSpScDetailPoster);
                    //3.处理数据
                    for(AdSpScDetailPoster adSpScDetailPoster1 : adSpScDetailPosters){
                        AdSpScDetailPosterVo adSpScDetailPosterVo = new AdSpScDetailPosterVo();
                        adSpScDetailPosterVo.setId(adSpScDetailPoster1.getId());
                        adSpScDetailPosterVo.setPosterUrl(adSpScDetailPoster1.getPosterUrl());
                        adSpScDetailPosterVo.setResolution(adSpScDetailPoster1.getResolution());

                        //将数据赋给列表内海报对象
                        adSpScDetailVo.setAdSpScDetailPosterVo(adSpScDetailPosterVo);
                    }
                }//视频
                else if (adSpScDetailVo.getType()==2){
                    //将id封装成视频广告对象
                    AdSpScDetailVideo adSpScDetailVideo = new AdSpScDetailVideo();
                    adSpScDetailVideo.setAdSpScDetailId(adSpScDetailVo.getId());

                    //查找数据
                    IAdSpScDetailVideoDao adSpScDetailVideoDao = new AdSpScDetailVideoDaoImpl();
                    List<AdSpScDetailVideo> adSpScDetailVideos = adSpScDetailVideoDao.searchAdSpScDetailVideo(adSpScDetailVideo);

                    //处理数据
                    for (AdSpScDetailVideo adSpScDetailVideo1 : adSpScDetailVideos) {
                        AdSpScDetailVideoVo adSpScDetailVideoVo = new AdSpScDetailVideoVo();
                        adSpScDetailVideoVo.setId(adSpScDetailVideo1.getId());
                        adSpScDetailVideoVo.setVideoUrl(adSpScDetailVideo1.getVideoUrl());
                        adSpScDetailVideoVo.setResolution(adSpScDetailVideo1.getResolution());
                        adSpScDetailVideoVo.setTotalTime(adSpScDetailVideo1.getTotalTime());

                        //将数据保存给视频对象
                        adSpScDetailVo.setAdSpScDetailVideoVo(adSpScDetailVideoVo);
                    }
                }

                adSpScDetailVos.add(adSpScDetailVo);
            }
            //将分屏明细列表赋值给广告主vo
            adInfoVo.setAdSpScDetailVoList(adSpScDetailVos);
            //将广告信息的插入列表
            adInfoVos.add(adInfoVo);

        }
        return adInfoVos;
    }

    /**
     * 下线广告
     * @param id 需要下线的id标识
     * @return是否成功
     */
    @Override
    public Boolean release(Long id) {
        //封装数据
        AdInfo adInfonew = new AdInfo();
        //判断是否符合条件（仅上线线的支持）
        //标记
        int flag = 0;
        IAdInfoDao adInfoDao = new AdInfoDaoImpl();
        //查找符合条件的数据
        adInfonew = adInfoDao.getAdInfoById(id);
        adInfonew.setStatus(1);
        AdInfo adInfoById = adInfoDao.getAdInfoById(id);
        if(adInfoById.getStatus() == 0){
            flag = adInfoDao.updateAdInfo(adInfonew);
        }else{
            throw new IllegalArgumentException("下线广告不支持下线");
        }

        return flag>0;
    }

    /**
     * 上线广告
     * @param id 需要上线的id标识
     * @return是否成功
     */
    @Override
    public Boolean offLine(Long id) {
        //封装数据
        AdInfo adInfonew = new AdInfo();
        //判断是否符合条件（仅上线线的支持）
        //标记
        int flag = 0;
        IAdInfoDao adInfoDao = new AdInfoDaoImpl();
        //查找符合条件的数据
        adInfonew = adInfoDao.getAdInfoById(id);
        adInfonew.setStatus(0);
        AdInfo adInfoById = adInfoDao.getAdInfoById(id);
        if(adInfoById.getStatus() == 1){
            flag = adInfoDao.updateAdInfo(adInfonew);
        }else{
            throw new IllegalArgumentException("下线广告不支持下线");
        }
        return flag>0;
    }
}
